<?php
class omeanalysts_mdl_ome_ldwardquery extends dbeav_model{

    //var $has_export_cnf = true;

    var $export_name = 'ldward查询';

    public function table_name($real=false){
        $table_name = 'ome_delivery';
        if($real){
            return kernel::database()->prefix.$table_name;
        }else{
            return $table_name;
        }
    }

    public function count($filter=null){

        $where = $this->_filter($filter); 

        $sql = "SELECT count(*) as _count from sdb_ome_reissuedoc rd 
                inner join sdb_ome_reissuedoc_line rdl on rd.id=rdl.reissuedoc_id
                inner join sdb_ome_orders os on rd.order_id=os.order_id
                inner join sdb_ome_shop f on os.shop_id=f.shop_id
                inner join sdb_ome_shop ff on rd.shop_id=ff.shop_id
                    where  ".$where." ";
        $row = $this->db->select($sql);
        return intval($row[0]['_count']);
    }

    public function getlist($cols='*', $filter=array(), $offset=0, $limit=-1, $orderType=null){
  

        if(!$filter['time_from'] && !$filter['time_to'])
        {
            return false;
        }else{           
            $where = $this->_filter($filter); 
            $sql = "SELECT f.shop_bn,f.name shop_name,ff.shop_bn issueshopbn,ff.name issueshopname,os.order_bn,
            rd.new_order_bn,os.createtime,rdl.bn,rdl.`name` product_name,
              rdl.num,rdl.price,rdl.effective issuenum,rd.reason,rd.create_time,rd.status from sdb_ome_reissuedoc rd 
                inner join sdb_ome_reissuedoc_line rdl on rd.id=rdl.reissuedoc_id
                inner join sdb_ome_orders os on rd.order_id=os.order_id
                inner join sdb_ome_shop f on os.shop_id=f.shop_id
                inner join sdb_ome_shop ff on rd.shop_id=ff.shop_id
                where  ".$where." "; 
            $rows = $this->db->selectLimit($sql,$limit,$offset);
            
            return $rows;
        }
        
    }
    
    public function searchOptions(){
        $parentOptions = parent::searchOptions();
        $childOptions = array(
            'order_bn'=>app::get('base')->_('订单号'),
        );
        return $Options = array_merge($parentOptions,$childOptions);
    }

    public function _filter($filter,$tableAlias=null,$baseWhere=null){

         $where = array(1);

       if(empty($filter['time_from']))
        {
          $where[]=' 1=2 ';
        }
        if(isset($filter['time_from']) && $filter['time_from']){
            $where[] = ' rd.create_time >='.strtotime($filter['time_from']);
        }
        if(isset($filter['time_to']) && $filter['time_to']){
            $where[] = 'rd.create_time <'.(strtotime($filter['time_to'])+86400);
        }
         
        if(isset($filter['shop_id'])   && $filter['shop_id']){
            $where[] = ' f.shop_id  =\''.addslashes($filter['shop_id']).'\'';
        }
        if(isset($filter['isshop_id'])   && $filter['isshop_id']){
            $where[] = ' rd.shop_id  =\''.addslashes($filter['isshop_id']).'\'';
        }
        if(!$filter['shop_id'] && $filter['shop_ids']){
            $where[] = ' f.shop_id IN (\''.implode('\',\'', $filter['shop_ids']).'\')';
        }
        if(isset($filter['order_bn']) && $filter['order_bn']){
            $where[] = ' os.order_bn =\''.addslashes($filter['order_bn']).'\'';
        }
        if(isset($filter['new_order_bn']) && $filter['new_order_bn']){
            $where[] = ' rd.new_order_bn =\''.addslashes($filter['new_order_bn']).'\'';
        }
        if(isset($filter['p_bn']) && $filter['p_bn']){
            $where[] = ' rdl.bn =\''.addslashes($filter['p_bn']).'\'';
        }
        if(isset($filter['status']) && $filter['status']){
            $where[] = ' rd.status =\''.addslashes($filter['status']).'\'';
        }
        unset($filter['shop_id']);
        
        return implode($where,' AND ');
    }

    public function get_schema(){
        $schema = array (
            'columns' => array (
                'shop_bn' =>
                array (
                  'type' => 'varchar(255)',
                  'required' => true,
                  'label' => '店铺编码',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '120',
                ), 
                'shop_name' =>
                array (
                  'type' => 'varchar(255)',
                  'required' => true,
                  'label' => '店铺',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '120',
                ), 
                'issueshopbn' =>
                array (
                  'type' => 'varchar(255)',
                  'required' => true,
                  'label' => '补发店铺编码',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '120',
                ), 
                'issueshopname' =>
                array (
                  'type' => 'varchar(255)',
                  'required' => true,
                  'label' => '补发店铺',
                  'editable' => false,
                  'searchtype' => 'has',
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                  'default_in_list' => true,
                  'is_title' => true,
                  'width' => '120',
                ), 
               'order_bn' =>
                array(
                    'type' => 'varchar(32)',
                    'required' => true,
                    'label' => '订单号',
                    'is_title' => true,
                    'width' => 125,
                    'searchtype' => 'nequal',
                    'editable' => false,
                    'filtertype' => 'normal',
                    'filterdefault' => true,
                    'in_list' => true,
                    'default_in_list' => false,
                ), 
               'new_order_bn' =>
                array(
                    'type' => 'varchar(32)',
                    'required' => true,
                    'label' => '补发订单号',
                    'is_title' => true,
                    'width' => 125,
                    'searchtype' => 'nequal',
                    'editable' => false,
                    'filtertype' => 'normal',
                    'filterdefault' => true,
                    'in_list' => true,
                    'default_in_list' => false,
                ), 
                'createtime' =>
                array (
                  'type' => 'time',
                  'required' => true,
                  'label' => '订单创建时间',
                  'is_title' => true,
                  'default_in_list' => true,
                  'width' => 70,
                  'searchtype' => 'has',
                  'editable' => false,
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                ),             
                 'bn' =>
                array (
                  'type' => 'varchar(30)',
                  'label' => '物料',
                  'editable' => false,
                  'is_title' => true,
                ),   
                 'product_name' =>
                array (
                  'type' => 'varchar(200)',
                  'required' => true,
                  'label' => '物料名称',
                  'is_title' => true,
                  'default_in_list' => true,
                  'width' => 260,
                  'searchtype' => 'has',
                  'editable' => false,
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                ),   
                'num' =>
                array (
                  'type' => 'number',
                  'label' => '补发量',
                  'required' => true,
                  'width' => 60,
                  'default' => 0,
                  'editable' => false,
                ),    
                 'price' =>
                array (
                  'type' => 'money',
                  'label' => '原价',
                  'width' => 70,
                  'required' => true,
                  'default' => 0,
                  'editable' => false,
                ),
                'issuenum' =>
                array (
                  'type' => 'number',
                  'label' => '购买量',
                  'width' => 60,
                  'is_title' => true,
                  'default_in_list' => true,
                  'searchtype' => 'has',
                  'editable' => false,
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                ),  
                'reason' =>
                array (
                  'type' => 'varchar(200)',
                  'label' => '补发原因',
                  'comment' => '补发原因',
                  'editable' => false,
                  'in_list' => true,
                  'width' => '120',
                ),  
                'create_time' =>
                array (
                  'type' => 'time',
                  'required' => true,
                  'label' => '补发时间',
                  'is_title' => true,
                  'default_in_list' => true,
                  'width' => 70,
                  'searchtype' => 'has',
                  'editable' => false,
                  'filtertype' => 'normal',
                  'filterdefault' => true,
                  'in_list' => true,
                ),  
              'status' =>
              array (
                'type' =>
                array (
                  'unconfirmed' => '新建',
                  'confirmed' => '已审核',
                  'cancel' => '取消',
                ),
                'default' => 'unconfirmed',
                'required' => true,
                'label' => '状态',
                'width' => 70,
                'editable' => false,
                'filtertype' => 'yes',
                'filterdefault' => true,
                'in_list' => true,
                'default_in_list' => false,
              ),          
              
            ),
            'idColumn' => 'name',
            'in_list' => array (
                0 => 'shop_bn',
                1 => 'shop_name',
                2 => 'issueshopbn',
                3 => 'issueshopname',
                4 => 'order_bn',
                5 => 'new_order_bn',
                6 => 'createtime',
                7 => 'bn',
                8 => 'product_name',
                9 => 'num',
                10 => 'price',
                11 => 'issuenum',
                12 => 'reason',
                13 => 'create_time',   
                14 => 'status',   

            ),
            'default_in_list' => array (
                0 => 'shop_bn',
                1 => 'shop_name',
                2 => 'issueshopbn',
                3 => 'issueshopname',
                4 => 'order_bn',
                5 => 'new_order_bn',
                6 => 'createtime',
                7 => 'bn',
                8 => 'product_name',
                9 => 'num',
                10 => 'price',
                11 => 'issuenum',
                12 => 'reason',   
                13 => 'create_time',
                14 => 'status',       
            ),
        );
        return $schema;
    }

    /**
     * 获得日志类型(non-PHPdoc)
     * @see dbeav_model::getLogType()
     */
    public function getLogType($logParams) {
        $type = $logParams['type'];
        $logType = 'none';
        if ($type == 'export') {
            $logType = $this->exportLogType($logParams);
        }
        elseif ($type == 'import') {
            $logType = $this->importLogType($logParams);
        }
        return $logType;
    }
    /**
     * 导出日志类型
     * @param Array $logParams 日志参数
     */
    public function exportLogType($logParams) {
        $params = $logParams['params'];
        $type = 'report';
        if ($logParams['app'] == 'omeanalysts' && $logParams['ctl'] == 'ome_delivery') {
            $type .= '_purchaseReport_expressAnalysis';
        }
        $type .= '_export';
        return $type;
    }
    /**
     * 导入操作日志类型
     * @param Array $logParams 日志参数
     */
    public function importLogType($logParams) {
        $params = $logParams['params'];
        $type = 'report';
        if ($logParams['app'] == 'omeanalysts' && $logParams['ctl'] == 'ome_delivery') {
            $type .= '_purchaseReport_expressAnalysis';
        }
        $type .= '_import';
        return $type;
    }

    function io_title( $filter, $ioType='csv' ){

        switch( $filter ){
            case 'delivery':
                $this->oSchema['csv'][$filter] = array(
                    '*:店铺' => 'shop_bn',
                    '*:店铺名称' => 'shop_name',
                    '*:补发店铺' => 'issueshopbn',
                    '*:补发店铺名称' => 'issueshopname',
                    '*:店铺' => 'shop_bn',
                    '*:店铺名称' => 'shop_name',
                    '*:订单号' => 'order_bn',
                    '*:补发订单号' => 'new_order_bn',
                    '*:订单日期' => 'createtime',
                    '*:物料' => 'bn',
                    '*:物料名称' => 'product_name',
                    '*:补发量' => 'num',
                    '*:原价' => 'price',
                    '*:购买量' => 'issuenum',
                    '*:原因' => 'reason',
                    '*:补发时间' => 'create_time',
                    '*:状态' => 'status',

                );
                break;
        }
        $this->ioTitle[$ioType][$filter] = array_keys( $this->oSchema[$ioType][$filter] );
        return $this->ioTitle[$ioType][$filter];
    }

    //根据查询条件获取导出数据
    public function getExportDataByCustom($fields, $filter, $has_detail, $curr_sheet, $start, $end){
          //根据选择的字段定义导出的第一行标题
          if($curr_sheet == 1){

              $title = array();
              foreach($this->io_title('delivery') as $k => $v ){
                  $title[] = mb_convert_encoding($v, 'GBK', 'UTF-8');
              }

              $data['content']['main'][0] = '"'.implode('","',$title).'"';
          }

          $where = $this->_filter($filter);

      $sql = "SELECT f.shop_bn,f.name shop_name,ff.shop_bn issueshopbn,ff.name issueshopname,os.order_bn,rd.new_order_bn,FROM_UNIXTIME(os.createtime, '%Y-%m-%d %H:%i:%S') as createtime,rdl.bn,rdl.`name` product_name,rdl.num,rdl.price,rdl.effective issuenum,rd.reason,FROM_UNIXTIME(rd.create_time, '%Y-%m-%d %H:%i:%S') as create_time,rd.status from sdb_ome_reissuedoc rd 
          inner join sdb_ome_reissuedoc_line rdl on rd.id=rdl.reissuedoc_id
          inner join sdb_ome_orders os on rd.order_id=os.order_id
          inner join sdb_ome_shop f on os.shop_id=f.shop_id
          inner join sdb_ome_shop ff on rd.shop_id=ff.shop_id
                where  ".$where." "; 
                $status=
                array (
                  'unconfirmed' => '新建',
                  'confirmed' => '已审核',
                  'cancel' => '取消',
                );
            $omedelivery = $this->db->selectLimit($sql,$end,$start);
            foreach ($omedelivery as $key => $value) 
            {
              $omedelivery[$key]=$status[$value['status']];
            }
            foreach ($omedelivery as $k => $aFilter) {
                $exptmp_data = array();
                foreach ($aFilter as $col => $val) {
                    $aFilter[$col] = mb_convert_encoding($aFilter[$col], 'GBK', 'UTF-8');
                    $exptmp_data[] = $aFilter[$col];
                }

                $data['content']['main'][] = implode(',', $exptmp_data);
            }
            return $data;
        
    }
}
